package net.trombonium.steroid;

import java.util.*;


public class Item {
	private int id;
	private String prompt;
	private ArrayList<Response> responses;
	private Map<String, Integer> nextItem;
	public static int SURVEY_END = -1;
	
	public  Item(int i, String p){
		id = i;
		prompt = p;
		responses = new ArrayList<Response>();
		nextItem = new HashMap<String, Integer>();
	}
	
	public void setNext(int nextId){
		nextItem.put(null, new Integer(nextId));
	}
	public void setNext(int nextId, String response){
		nextItem.put(response, new Integer(nextId));
	}
	public void setNexts(String str_nexts){
		String[] nexts = str_nexts.split(";");
		for(int i=0; i<nexts.length; ++i){
			String[] next = nexts[i].split(",");
			if(next.length==1)
				setNext(Integer.parseInt(next[0]));
			else
				setNext(Integer.parseInt(next[0]), next[1]);
		}
	}
	public void addResponse(int type){
		responses.add(new Response(responses.size(), type));
	}
	public void addResponses(String str_types){
		String[] types = str_types.split(",");
		for(int i=0; i<types.length; ++i)
			addResponse(Integer.parseInt(types[i]));
	}
	
	//Accessors
	public String getPrompt(){ return prompt; }
	public int getId(){ return id; }
	public int getNextId(String response){
		if(nextItem.isEmpty())
			return id+1;
		if(nextItem.containsKey(response))
			return nextItem.get(response).intValue();
		return nextItem.get(null);
	}
	public void recordResponse(int id, String value){
		responses.get(id).value = value;
	}
	public int getResponseNum(){
		return responses.size();
	}
	public Response getResponse(int id){
		return responses.get(id);
	}
	
}
